package com.lagou.controller;

import com.lagou.domain.Employee;
import com.lagou.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;

@Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @RequestMapping("/findAll")
    public String findAll(Model model){

        List<Employee> emp = employeeService.findAll();
        model.addAttribute("list", emp);
        return "list";
    }

    @RequestMapping("/save")
    public String save(Employee employee){

        employeeService.save(employee);
        return "redirect:/empWithDept/findAll";
    }

    @RequestMapping("/deleteBatch")
    public String deleteBatch(Integer[] ids){

        employeeService.deleteBatch(ids);
        return "redirect:/empWithDept/findAll";
    }

    @RequestMapping("/findById")
    public String findById(Integer emp_id, Model model){

        Employee employee = employeeService.findById(emp_id);
        model.addAttribute(employee);
        return "update";
    }

    @RequestMapping("/update")
    public String update(Employee employee){

        System.out.println(employee);
        employeeService.update(employee);
        return "redirect:/empWithDept/findAll";
    }
}
